﻿using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Reflection;
using ZHDJ.Data.Mapping.Center;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.ATM;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Box;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Edu;
using ZHDJ.Data.Models.Forum;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Data.Models.MassLine;
using ZHDJ.Data.Models.Org;
using ZHDJ.Data.Models.Questionnaire;
using ZHDJ.Data.Models.SMS;
using ZHDJ.Data.Models.Donation;
using ZHDJ.Data.Models.SeekHelp;

namespace ZHDJ.Data
{
    public class AppBoxContext : DbContext
    {
        //public AppBoxContext() : base("MySQL")
        //{
        //}
        #region  数据表

        public DbSet<Config> Configs { get; set; }
        #region 组织架构
        /// <summary>
        ///组织信息
        /// </summary>
        public DbSet<PartyOrgBranch> PartyOrgBranchs { get; set; }
        /// <summary>
        /// 组织基础信息配置 
        /// </summary>
        public DbSet<PartyOrgBranchSetting> PartyOrgBranchSettings { get; set; }
        /// <summary>
        /// 组织管理员
        /// </summary>
        public DbSet<PartyOrgBranchAdmin> PartyOrgBranchAdmins { get; set; }
        /// <summary>
        /// 组织机构职责
        /// </summary>
        public DbSet<PartyOrgDuty> PartyOrgDutys { get; set; }
        /// <summary>
        /// 人员信息
        /// </summary>
        public DbSet<PartyOrgMember> PartyOrgMembers { get; set; }

        /// <summary>
        /// 历史人员信息
        /// </summary>
        public DbSet<PartyOrgMemberHistory> PartyOrgMemberHistorys { get; set; }
        /// <summary>
        /// 人员基础信息配置 
        /// </summary>
        public DbSet<PartyOrgMemberSetting> PartyOrgMemberSettings { get; set; }

        #endregion
        public DbSet<Dept> Depts { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Title> Titles { get; set; }
        public DbSet<Online> Onlines { get; set; }
        public DbSet<Log> Logs { get; set; }
        public DbSet<Power> Powers { get; set; }
        public DbSet<Menu> Menus { get; set; }

        /// <summary>
        /// 门户菜单
        /// </summary>
        public DbSet<PortalMenu> PortalMenus { get; set; }

        /// <summary>
        /// 门户菜单APP
        /// </summary>
        public DbSet<PortalMoveMenu> PortalMoveMenus { get; set; }

        /// <summary>
        /// 关键字
        /// </summary>
        public DbSet<KeyWord> KeyWords { get; set; }

        /// <summary>
        /// 页面模块菜单
        /// </summary>
        public DbSet<PartyModuleNavigation> PartyModuleNavigations { get; set; }
        /// <summary>
        /// 专题
        /// </summary>
        public DbSet<PartyEduSpecial> PartyEduSpecials { get; set; }

        #region 学习测试
        /// <summary>
        /// 题目
        /// </summary>
        public DbSet<PartyEduSubject> PartyEduSubjects { get; set; }

        /// <summary>
        /// 题目选项
        /// </summary>
        public DbSet<PartyEduSubjectOptions> PartyEduSubjectOptions { get; set; }

        /// <summary>
        /// 学习测试记录
        /// </summary>
        public DbSet<PartyEduTestRecord> PartyEduTestRecords { get; set; }

        /// <summary>
        /// 学习测试记录详细
        /// </summary>
        public DbSet<PartyEduTestRecordDetail> PartyEduTestRecordDetails { get; set; }

        /// <summary>
        /// 学习课程
        /// </summary>
        public DbSet<PartyEduLesson> PartyEduLessons { get; set; }

        /// <summary>
        /// 学习课程图片
        /// </summary>
        public DbSet<PartyeduLessonimage> PartyEduLessonsImages { get; set; }


        /// <summary>
        /// 学习课程课件
        /// </summary>
        public DbSet<PartyeduLessonCourseRelation> PartyeduLessonCourseRelations { get; set; }

        /// <summary>
        /// 学习课件
        /// </summary>
        public DbSet<PartyEduCourseware> PartyEduCoursewares { get; set; }

        /// <summary>
        /// 学习课件图片
        /// </summary>
        public DbSet<PartyEduCoursewareImage> PartyEduCoursewareImages { get; set; }
        #endregion

        #region 文章
        /// <summary>
        /// 文章
        /// </summary>
        public DbSet<PartyArticle> PartyArticles { get; set; }

        /// <summary>
        /// 文章附件
        /// </summary>
        public DbSet<PartyArticlesFile> PartyArticlesFiles { get; set; }
        #endregion

        #region 留影墙
        /// <summary>
        /// 图片库
        /// </summary>
        public DbSet<PartyPhotoWall> PartyPhotoWalls { get; set; }

        /// <summary>
        /// 图片库的图片
        /// </summary>
        public DbSet<PartyPhotoWallImage> PartyPhotoWallImages { get; set; }
        #endregion
        #region 基础类
        /// <summary>
        /// 上传文件信息
        /// </summary>
        public DbSet<PartyFile> PartyFiles { get; set; }

        /// <summary>
        /// 数据字典
        /// </summary>
        public DbSet<DataDictionary> DataDictionarys { get; set; }

        /// <summary>
        /// 系统参数
        /// </summary>
        public DbSet<SystemParameter> SystemParameters { get; set; }
        #endregion

        #region 入党登记
        public DbSet<JoinPartyRegister> JoinPartyRegisters { get; set; }
        #endregion
        #region 入党流程
        /// <summary>
        /// 入党申请
        /// </summary>
        public DbSet<PartyJoinApplycation> PartyJoinApplycation { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public DbSet<PartyJoinChildFlow> PartyJoinChildFlows { get; set; }
        public DbSet<partyJoinContact> partyJoinContacta { get; set; }
        public DbSet<PartyJoinFile> PartyJoinFiles { get; set; }
        public DbSet<PartyJoinFlowInfoHelper> PartyJoinFlowInfoHelpers { get; set; }
        public DbSet<PartyJoinLinkExample> PartyJoinLinkExamples { get; set; }
        public DbSet<partyJoinMeeting> partyJoinMeetings { get; set; }
        public DbSet<PartyJoinMeetingBackstep> PartyJoinMeetingBacksteps { get; set; }
        public DbSet<PartyJoinOrganizationOathRelation> PartyJoinOrganizationOathRelations { get; set; }
        public DbSet<partyJoinPartyActivists> partyJoinPartyActivistss { get; set; }
        public DbSet<PartyJoinPartyApply> PartyJoinPartyApplys { get; set; }
        public DbSet<PartyJoinPartyRule> PartyJoinPartyRules { get; set; }
        public DbSet<PartyJoinProbationaryResume> PartyJoinProbationaryResumes { get; set; }
        public DbSet<PartyJoinProcess> PartyJoinProcesss { get; set; }
        public DbSet<partyjoinProcessBack> partyjoinProcessBacks { get; set; }
        public DbSet<PartyJoinPromise> PartyJoinPromises { get; set; }
        public DbSet<PartyJoinReviewOption> PartyJoinReviewOptions { get; set; }
        public DbSet<PartyJoinSubmit> PartyJoinSubmits { get; set; }
        public DbSet<PartyJoinSworn> PartyJoinSworns { get; set; }
        public DbSet<PartyJoinThoughtReport> PartyJoinThoughtReports { get; set; }

        public DbSet<PartyProbationaryPartyMember> PartyProbationaryPartyMembers { get; set; }

        public DbSet<partyjointrained> partyjointraineds { get; set; }

        public DbSet<partyjoinMeetingFive> partyjoinMeetingFives { get; set; }

        public DbSet<partyJoinMeetingEx> partyJoinMeetingExs { get; set; }

        public DbSet<partyjoinexamination> partyjoinexaminations { get; set; }

        public DbSet<partyjoinpublicity> partyjoinpublicitys { get; set; }

        public DbSet<PartyRelationnshipPublicity> PartyRelationnshipPublicitys { get; set; }
        #endregion

        #region 积分详细
        /// <summary>
        /// 积分详细 
        /// </summary>
        public DbSet<PartyObjectScore> PartyObjectScores { get; set; }

        /// <summary>
        /// 积分申请 
        /// </summary>
        public DbSet<PartyObjectScoreApply> PartyObjectScoreApplys { get; set; }
        /// <summary>
        /// 历史积分详细 
        /// </summary>
        public DbSet<PartyObjectScoreHistory> PartyObjectScoreHistorys { get; set; }

        /// <summary>
        /// 积分规则
        /// </summary>
        public DbSet<PartyObjectScoreRule> PartyObjectScoreRules { get; set; }

        /// <summary>
        /// 积分规则描述
        /// </summary>
        public DbSet<PartyObjectScoreRuleDesc> PartyObjectScoreDescs { get; set; }
        #endregion
        /// <summary>
        /// 待办已办
        /// </summary>
        public DbSet<PartyTask> PartyTasks { get; set; }

        /// <summary>
        /// 图说党支部
        /// </summary>
        public DbSet<PartyBranchPictureShow> PartyBranchPictureShows { get; set; }

        /// <summary>
        /// 流动党员找党组织
        /// </summary>
        public DbSet<PartyMembersFindOrg> PartyMembersFindOrgs { get; set; }

        /// <summary>
        /// 政民互动
        /// </summary>
        public DbSet<PartyMessageBox> PartyMessageBoxs { get; set; }

        /// <summary>
        /// 党费缴纳
        /// </summary>
        public DbSet<PartyMemberCost> PartyMemberCosts { get; set; }

        #region 阵地管理
        /// <summary>
        /// 预约阵地
        /// </summary>
        public DbSet<PartyPossyAppointment> PartyPossyAppointments { get; set; }
        /// <summary>
        /// 阵地管理发布表
        /// </summary>
        public DbSet<PartyPossyInfo> PartyPossyInfos { get; set; }
        /// <summary>
        /// 阵地管理审核表
        /// </summary>
        public DbSet<PartyPossyInfoAduit> PartyPossyInfoAduits { get; set; }

        #endregion

        #region 组织关系转移
        /// <summary>
        /// 组织关系转移表单
        /// </summary>
        public DbSet<PartyRelationshipTransfer> PartyRelationshipTransfers { get; set; }
        /// <summary>
        /// 组织关系转移审核步骤详情
        /// </summary>
        public DbSet<PartyRelationshipProcesses> PartyRelationshiptblProcessess { get; set; }
        /// <summary>
        /// 组织关系流程步骤定义
        /// </summary>
        public DbSet<PartyRelationshipTageBranch> PartyRelationshipTageBranchs { get; set; }

        /// <summary>
        /// 评论表
        /// </summary>
        public DbSet<PartyComment> PartyComments { get; set; }

        #endregion

        #region 群众路线
        /// <summary>
        /// 群众意见收集栏
        /// </summary>
        public DbSet<PartyMassLineFeedback> PartyMassLineFeedbacks { get; set; }

        public DbSet<PartyMasslineFeedbackOption> PartyMasslineFeedbackOptions { get; set; }

        public DbSet<PartyMassLineSuggest> PartyMassLineSuggests { get; set; }

        public DbSet<PartyActivityDynamic> PartyActivityDynamics { get; set; }
        public DbSet<PartyBranchDraft> PartyBranchDrafts { get; set; }

        public DbSet<PartyBranchStudyActivitie> PartyBranchStudyActivities { get; set; }


        #endregion

        /// <summary>
        /// 历史上的今天
        /// </summary>
        public DbSet<PartyHistoryToday> PartyHistoryTodays { get; set; }

        /// <summary>
        /// 书记管理
        /// </summary>
        public DbSet<PartySecretary> PartySecretarys { get; set; }

        /// <summary>
        /// 干部工作 意见建议
        /// </summary>
        public DbSet<PartyCadresWorkSuggest> PartyCadresWorkSuggests { get; set; }
        #endregion

        #region 活动
        /// <summary>
        /// 支部活动
        /// </summary>
        public DbSet<PartyBranchActivitie> PartyBranchActivities { get; set; }
        /// <summary>
        /// 支部活动参与人员
        /// </summary>
        public DbSet<PartyBranchActivitieMember> PartyBranchActivitieMembers { get; set; }

        /// <summary>
        /// 三会一课
        /// </summary>
        public DbSet<PartyMeetingLecture> PartyMeetingLectures { get; set; }
        /// <summary>
        /// 三会一课参与人员
        /// </summary>
        public DbSet<PartyMeetingLectureMember> PartyMeetingLectureMembers { get; set; }
        #endregion

        #region 党代表
        /// <summary>
        /// 党代表工作室
        /// </summary>
        public DbSet<PartyRepresentativeStudio> PartyRepresentativeStudios { get; set; }

        /// <summary>
        /// 党代表信息
        /// </summary>
        public DbSet<PartyRepresentativeInfo> PartyRepresentativeInfos { get; set; }

        /// <summary>
        /// 党代表工作室活动公告
        /// </summary>
        public DbSet<PartyRepresentativeMeeting> PartyRepresentativeMeetings { get; set; }

        /// <summary>
        /// 党代表工作室活动：参与党代表
        /// </summary>
        public DbSet<PartyRepresentativeMeetingJoin> PartyRepresentativeMeetingJoins { get; set; }
        /// <summary>
        /// 约见党代表
        /// </summary>
        public DbSet<PartyRepresentativeMeetingWith> PartyRepresentativeMeetingWiths { get; set; }

        /// <summary>
        /// 党代表心声传递
        /// </summary>
        public DbSet<PartyRepresentativeSuggestion> PartyRepresentativeSuggestions { get; set; }

        #endregion

        #region 志愿活动
        /// <summary>
        /// 志愿活动
        /// </summary>
        public DbSet<PartyVolunteerActivitie> PartyVolunteerActivities { get; set; }

        /// <summary>
        /// 志愿活动
        /// </summary>
        public DbSet<PartyVolunteerActivitieMember> PartyVolunteerActivitieMembers { get; set; }

        /// <summary>
        /// 志愿者心语
        /// </summary>
        public DbSet<PartyVolunteerQuotation> PartyVolunteerQuotations { get; set; }

        /// <summary>
        /// 领导班子
        /// </summary>
        public DbSet<PartyOrgLeader> PartyOrgLeaders { get; set; }

        /// <summary>
        /// 领导之窗
        /// </summary>
        public DbSet<PartyOrgLeaderWindow> PartyOrgLeaderWindows { get; set; }

        #endregion

        #region ATM
        public DbSet<PartyATMSaveAndTake> PartyATMSaveAndTakes { get; set; }

        public DbSet<PartyATMSaveThings> PartyATMSaveThingss { get; set; }

        public DbSet<PartyATMTakeThings> PartyATMTakeThingss { get; set; }
        #endregion

        #region 飘窗

        public DbSet<PartyFloatWindow> FloatWindows { get; set; }

        #endregion

        #region 论坛
        /// <summary>
        /// 帖子
        /// </summary>
        public DbSet<PartyForum> PartyForums { get; set; }
        /// <summary>
        /// 帖子浏览记录
        /// </summary>
        public DbSet<PartyForumBrowse> PartyForumBrowses { get; set; }
        /// <summary>
        /// 回帖
        /// </summary>
        public DbSet<PartyForumReply> PartyForumReplys { get; set; }
        /// <summary>
        /// 论坛版块
        /// </summary>
        public DbSet<PartyForumType> PartyForumTypes { get; set; }
        /// <summary>
        /// 论坛版块信息统计
        /// </summary>
        public DbSet<PartyForumTypeStatistics> PartyForumTypeStatisticss { get; set; }

        /// <summary>
        /// 论坛版主设置表
        /// </summary>
        public DbSet<PartyForumTypeAdmin> PartyForumTypeAdmins { get; set; }
        /// <summary>
        /// 用户论坛等级
        /// </summary>
        public DbSet<PartyForumUserInfo> PartyForumUserInfos { get; set; }
        /// <summary>
        /// 论坛等级配置
        /// </summary>
        public DbSet<PartyForumUpgradeSetting> PartyForumUpgradeSettings { get; set; }
        #endregion

        #region 日志
        /// <summary>
        /// 用户登陆日志
        /// </summary>
        public DbSet<PartyUserLoginLog> PartyUserLoginLogs { get; set; }

        /// <summary>
        /// 访问日志
        /// </summary>
        public DbSet<PartyTouristVisit> PartyTouristVisits { get; set; }

        /// <summary>
        /// 访问统计
        /// </summary>
        public DbSet<PartyVisitStatistic> PartyVisitStatistics { get; set; }
        #endregion

        #region 领导驻点
        /// <summary>
        /// 驻点分组
        /// </summary>
        public DbSet<PartyGrassrootsGroup> PartyGrassrootsGroups { get; set; }

        /// <summary>
        /// 驻点小组
        /// </summary>
        public DbSet<PartyGrassrootsGroupItem> PartyGrassrootsGroupItems { get; set; }

        /// <summary>
        /// 小组类型（所属管理区）
        /// </summary>
        public DbSet<PartyGrassrootsGroupItemType> PartyGrassrootsGroupItemTypes { get; set; }

        /// <summary>
        /// 小组成员
        /// </summary>
        public DbSet<PartyGrassrootsGroupItemMember> PartyGrassrootsGroupItemMembers { get; set; }
        /// <summary>
        /// 月报
        /// </summary>
        public DbSet<PartyGrassrootsGroupReport> PartyGrassrootsGroupReports { get; set; }
        /// <summary>
        /// 群众心声
        /// </summary>
        public DbSet<PartyGrassrootsSuggestion> PartyGrassrootsSuggestions { get; set; }
        /// <summary>
        /// 群众心声详情
        /// </summary>
        public DbSet<PartyGrassrootsSuggestionDetail> PartyGrassrootsSuggestionDetails { get; set; }
        /// <summary>
        /// 工作纪实
        /// </summary>
        public DbSet<PartyGrassrootsWorkTrend> PartyGrassrootsWorkTrends { get; set; }
        #endregion

        #region 问卷调查
        /// <summary>
        /// 调查问卷
        /// </summary>
        public DbSet<PartyQuestionnaire> PartyQuestionnaires { get; set; }
        /// <summary>
        /// 问卷问题选项(调查问卷)
        /// </summary>
        public DbSet<PartyQuestionnaireOption> PartyQuestionnaireOptions { get; set; }
        /// <summary>
        /// 用户提交记录(调查问卷)
        /// </summary>
        public DbSet<PartyQuestionnaireRecord> PartyQuestionnaireRecords { get; set; }
        /// <summary>
        /// 用户提交详细(调查问卷)
        /// </summary>
        public DbSet<PartyQuestionnaireRecordDetail> PartyQuestionnaireRecordDetails { get; set; }
        /// <summary>
        /// 问卷题目(调查问卷)
        /// </summary>
        public DbSet<PartyQuestionnaireSubject> PartyQuestionnaireSubjects { get; set; }
        #endregion

        #region 摄像机
        /// <summary>
        /// 摄像机
        /// </summary>
        public DbSet<partyvideoprobe> partyvideoprobes { get; set; }
        #endregion

        /// <summary>
        /// 定时作业
        /// </summary>
        public DbSet<ScheduledJob> ScheduledJobs { get; set; }

        #region 留言板
        public DbSet<PartyMessageBoard> PartyMessageBoards { get; set; }
        #endregion

        #region 短信留言板
        public DbSet<PartySmsmoRecord> PartySmsmoRecords { get; set; }
        #endregion

        #region 捐助
        public DbSet<PartyDonation> PartyDonations { get; set; }
        #endregion

        #region 求助
        public DbSet<PartySeekHelp> PartySeekHelps { get; set; }
        #endregion

        #region 短信
        public DbSet<PartySmsSendRecord> PartySmsSendRecords { get; set; }
        #endregion

        #region 创建映射关系
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
          .Where(type => !String.IsNullOrEmpty(type.Namespace))
          .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);

            //指定单数形式的表名
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //物理表名添加xx前綴
            modelBuilder.Types().Configure(f => f.ToTable("tbl" + f.ClrType.Name));

            modelBuilder.Entity<Role>()
                .HasMany(r => r.Users)
                .WithMany(u => u.Roles)
                .Map(x => x.ToTable("tblRoleUsers")
                    .MapLeftKey("RoleID")
                    .MapRightKey("LoginID"));

            modelBuilder.Entity<Role>()
                .HasMany(r => r.Powers)
                .WithMany(p => p.Roles)
                .Map(x => x.ToTable("tblRolePowers")
                    .MapLeftKey("RoleID")
                    .MapRightKey("PowerID"));

            modelBuilder.Entity<Title>()
                .HasMany(t => t.Users)
                .WithMany(u => u.Titles)
                .Map(x => x.ToTable("tblTitleUsers")
                    .MapLeftKey("TitleID")
                    .MapRightKey("LoginID"));


            modelBuilder.Entity<Online>()
                .HasRequired(o => o.User)
                .WithMany()
                .Map(x => x.MapKey("LoginID"));

            modelBuilder.Entity<Menu>()
                .HasOptional(m => m.Parent)
                .WithMany(m => m.Children)
                .Map(x => x.MapKey("ParentID"));



            modelBuilder.Entity<Menu>()
                .HasOptional(m => m.ViewPower)
             .WithMany()
              .HasForeignKey(a => a.ViewPowerID).WillCascadeOnDelete(false);
        }
        #endregion
    }
}